#include "stdio.h"
#include "fcntl.h"
#include "user.h"
#include <stdlib.h>
#define CONSOLE 1

char *argv[] = {"app_helloworld",0};
int
main(void)
{

  int pid, wpid,ret;

  if((ret=do_open("app_helloworld", O_RDWR)) < 0){
      printf("do_open fail call do_mknod\n");
      do_mknod("console", CONSOLE, 0);
  }
    printf("do_open ret = %d \n",ret);
	int a,b;
	if(ret >= 0)
	{
		a = dup(0); //stdout
		b = dup(0); //stderr
		printf("%d %d\n",a,b);
	}
	
	for(;;){
		printf("init:starting test exec\n");
		pid = fork();
		if(pid < 0){
			printf("init:fork failed\n");
			exit(1);
		}
		if(pid == 0){
			do_exec("app_helloworld",argv);
			printf("init:exec sh failed\n");
			exit(1);
		}
		for(;;){
			wpid = wait((int *)0);
			if(wpid == pid){
				break;
			} else if(wpid < 0){
				printf("init: wait returned an error\n");
				exit(1);
			} else {
					break;
			}
		}
	}

	return 0; 
}
